package cn.shyouchi.wuguanjia.android.ui.search.thread;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseFragment;
import cn.shyouchi.wuguanjia.android.ui.search.SearchResMod;
import cn.shyouchi.wuguanjia.android.ui.search.SearchResultAdapter;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchResultFragment extends BaseFragment {


    @BindView(R.id.rv_result)
    RecyclerView mRvResult;


    SearchResultAdapter mAdapter;

    List<SearchResMod> mResMods = new ArrayList<>();

    public SearchResultFragment() {

    }

    private static SearchResultFragment mFragment;

    public static SearchResultFragment instance() {
        if (mFragment == null) {
            synchronized (SearchResultFragment.class) {
                if (mFragment == null) {
                    mFragment = new SearchResultFragment();
                }
            }
        }
        return mFragment;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public int initContentView() {
        return R.layout.fragment_search_result;
    }

    @Override
    public void getBundle(Bundle bundle) {

    }

    @Override
    public void initUI(View view) {
        ButterKnife.bind(this, view);
        showProgress(true);
        mRvResult.setLayoutManager(new LinearLayoutManager(getContext()));

        mAdapter = new SearchResultAdapter(mResMods);
        mAdapter.openLoadAnimation();
        mRvResult.setAdapter(mAdapter);
    }

    @Override
    public void initData() {

    }

    public void search(String category, String keyWord) {
        showProgress(true);
        Observable.timer(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onCompleted() {
                for (int i = 0; i < 50; i++) {
                    mResMods.add(new SearchResMod("上海优驰保安有限公司"));
                }
                showContent(false);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {

            }
        });
    }
}
